package com.itheima.web.response;

import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.读取文件
        FileInputStream fis = new FileInputStream("c://Users//Odyssey//Pictures//view//star.jpg");

        //2.获取response字节输出流
        ServletOutputStream sos = response.getOutputStream();

        //3.完成流的copy
        /*byte[] buff=new byte[1024];
        int len = 0;
        while ((len=fis.read(buff))!=-1){
            sos.write(buff,0,len);
        }
        fis.close();*/

        //3 apache工具类实现copy
        IOUtils.copy(fis, sos);
        fis.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}